<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: table_mobile_setting.php 31281 2012-08-03 02:29:27Z zhangjie $
     */

    if (!defined('IN_DISCUZ')) {
        exit('Access Denied');
    }


    class table_cloud_category extends discuz_table {

        public function __construct() {
            $this->_table = 'cloud_category';
            $this->_pk = 'ccid';

            parent::__construct();
        }

        //插入数据库
        public function insert($arr) {
            return DB::insert($this->_table, $arr, true);
        }

        //更新数据库
        public function update($id, $data) {
            $id = intval($id);
            if (!$data) {
                return;
            }
            return DB::update($this->_table, $data, DB::field($this->_pk, $id));
        }

        //查找某条记录
        public function fetch_info($id) {
            $id = intval($id);
            return DB::fetch_first("SELECT * FROM %t WHERE ".$this->_pk."=%d", array($this->_table, $id));
        }
        //根据keys称查询
        public function fetch_category_all($cadid) {
              return DB::fetch_all("SELECT * FROM %t WHERE cadid=%d  order by ".DB::order('display').",".DB::order('lasttime','desc'), array($this->_table, $cadid));
        }
        //根据keys称查询
        public function fetch_category_all_by_enable($cadid,$start = 0, $limit = 0) {
            return DB::fetch_all("SELECT * FROM %t WHERE cadid=%d  and enable =1 order by ".DB::order('display').",".DB::order('lasttime','desc').DB::limit($start, $limit), array($this->_table, $cadid));
        }
        //提供下拉列表
        public function fetch_category_select($cadid) {
            return DB::fetch_all('SELECT name as text,ccid as value  FROM ' . DB::table($this->_table) .' where cadid = '.$cadid);
        }
        //查询所有limt数据
        public function fetch_all_limit($start = 0, $limit = 0) {
            return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) .' order by '.DB::order('lasttime','desc'). DB::limit($start, $limit));
        }
        
        // 删除
        public function delete_by_id($id) {
            $sql_category = 'SELECT cpid FROM  ' . DB::table('cloud_product_category') . ' where ccid = ' . $id;
            $category_product = DB::fetch_all($sql_category);
            if(!empty($category_product)){
                    $forCategory = '';
                    foreach ($category_product as $val) {
                        $forCategory .= $val['cpid'] . ',';
                    }
                    $forCategory = substr($forCategory, 0, strlen($forCategory) - 1);
                DB::query("UPDATE FROM ".DB::table("cloud_product")." set disabled =0 WHERE  cpid=in (" . $forCategory . ") ");
            }
            return DB::query("UPDATE FROM %t set enable=0  WHERE ".$this->_pk."=%d", array($this->_table, $id));
	}
        //查询count
        public function fetch_all_count($id) {
            return DB::fetch_first('SELECT count(ccid) as count FROM ' . DB::table($this->_table).' where cadid='.$id);
        }

    }